package de.au.fi.imp;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import de.au.fi.domain.Stock;
import de.au.fi.domain.StockIdMapping;
import de.au.fi.domain.StockIdMappingHelper;
import de.au.fi.imp.reader.GrahamInvestorReader;
import de.au.fi.imp.reader.MagicFormulaReader;
import de.au.fi.imp.reader.YahooFinanceIsinReader;
import de.au.fi.imp.reader.yahoodata.YahooStockImporter;
import de.au.fi.manager.StockIdMappingManager;

@Component
@Service
public class Importer {
	
	Logger log = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	StockIdMappingManager stockIdMappingManager;
	
	public List<Stock> getMagicStocks() throws Exception
	{
		MagicFormulaReader formulaReader = new MagicFormulaReader();
		List<String> magicTickers = (new ArrayList<String>(formulaReader.getMagicStocks().values())) ;
		formulaReader.destroy();
		List<Stock> stocks = getStocksForTicker(magicTickers);
		for (Stock stock : stocks) 
			stock.setMagicFormula(true);
		return stocks;
	}

	public List<Stock> getStocksForTicker(List<String> tickers) throws Exception {
		return importTickers(tickers.toArray(new String[tickers.size()]));
	}
	
	public List<Stock> importTickers(String... tickers) throws Exception 
	{
		List<Stock> stocks = getStocksForTicker(tickers);
		
		addPiotroskiAndAltmannZScore(stocks);
		
		manageIdMappings(stocks);
		
		addOnVistaData(stocks);
		
		return stocks;
	}


	public void addOnVistaData(List<Stock> stocks) {
		OnVistaImporter onVistaImporter = new OnVistaImporter();
		stocks = onVistaImporter.importStocksFromOnVista(stocks
				.toArray(new Stock[stocks.size()]));
		onVistaImporter.destroyReader();
	}


	public void addPiotroskiAndAltmannZScore(List<Stock> stocks) {
		GrahamInvestorReader grahamInvestorReader = new GrahamInvestorReader();
		stocks = grahamInvestorReader.addPiotroskiAndAltmannScore(stocks);
		grahamInvestorReader.destroy();
	}


	public List<Stock> getStocksForTicker(String... tickers) throws Exception {
		YahooStockImporter yahooStockImporter = new YahooStockImporter();
		List<Stock> stocks = yahooStockImporter.importYahooStocks(tickers);
		return stocks;
	}

	
	public void manageIdMappings(List<Stock> stocks) {
		YahooFinanceIsinReader yahooFinanceIsinReader = new YahooFinanceIsinReader();
		for(Stock stock : stocks)
		{
			if(stock.getIsin()==null)
			{
				StockIdMapping stockIdMapping = null;
				if(stockIdMappingManager.findByTickerYahooData(stock.getTicker())!=null)
					stockIdMapping = stockIdMappingManager.findByTickerYahooData(stock.getTicker());
				if(stockIdMapping==null)
					try {
						stockIdMapping = yahooFinanceIsinReader.getIdMappingsForTickers(stock.getTicker()).get(0);
						stockIdMappingManager.save(stockIdMapping);
					} catch (Exception e) {
						log.error("Could not do idMapping for {}", stock.getTicker());
						continue;
					}
				stock = StockIdMappingHelper.mapStockIdMappingToStock(stock, stockIdMapping);
			}
				
		}
		yahooFinanceIsinReader.destroy();
		
	}
}
